//
//  PerMessageViewController.m
//  sklive
//
//  Created by Macx on 2017/6/29.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "PerMessageViewController.h"
#import "PerMessageDetailsViewController.h"
#import "MessageTableViewCell.h"
#import "Message.h"

@interface PerMessageViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) MessageTableViewCell *cell;
@property (nonatomic, strong) NSMutableArray *infoArr;

@property (nonatomic, assign) NSInteger page; // 网络请求页数
@end

@implementation PerMessageViewController

- (void)viewWillAppear:(BOOL)animated {
    
    // 获取获取站内信列表
    [self GETMessage];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 进入请求为1
    self.page = 1;
    
    [self buildUI];
    
//    [self refreshData];
    
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    self.navigationItem.title = @"站内信消息";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}
#pragma mark ==========更新数据==========
- (void)refreshData {
    __weak typeof (self) weakSelf = self;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"下拉刷新");
        weakSelf.page = 1;
        [weakSelf GETMessage];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSLog(@"上拉加载");
        weakSelf.page++;
        [weakSelf GETMessage];
    }];
    
}

#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    Message *model = self.infoArr[indexPath.row];
    
    self.cell.model = model;
    
//    if (model.is_read == 1) {
        self.cell.num.hidden = YES;
//    }
    
    return self.cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SW(7)+20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView.cell = %ld", indexPath.row);
    
    Message *model = self.infoArr[indexPath.row];
    
    PerMessageDetailsViewController *detailVC = [[PerMessageDetailsViewController alloc] init];
    detailVC.mId = [NSString stringWithFormat:@"%@", [model valueForKey:@"mId"]];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark ==========响应方法==========
/**
 获取站内信列表
 */

- (void)GETMessage {
    
    [[NetworkTool sharedTool] requestWithName:@"获取站内信列表" URLString:APIMessages parameters:nil method:@"GET" callBack:^(id responseObject) {
        
        if (self.page == 1) {
            [self.infoArr removeAllObjects];
        }
        for (NSDictionary *dic in responseObject[@"data"]) {
            NSMutableDictionary *dd = [[NSMutableDictionary alloc] initWithDictionary:dic];
//            [dd removeObjectForKey:@"create_time"];
            Message *model = [[Message alloc] init];
            [model setValuesForKeysWithDictionary:dd];
            [self.infoArr addObject:model];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView reloadData];
        });
    }];
}

// 懒加载
- (NSMutableArray *)infoArr {
    if (_infoArr == nil) {
        _infoArr = [[NSMutableArray alloc] init];
    }
    return _infoArr;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
